﻿using Microsoft.EntityFrameworkCore;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;

namespace MiniFox.Data.EntityFramework.MySql
{
    /// <summary>
    /// 
    /// </summary>
    public class MySqlDbContext : DbContextBase
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="connectionName"></param>
        /// <param name="options"></param>
        public MySqlDbContext(string connectionName, DbContextOptions options) : this(connectionName, 30, options)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="connectionName"></param>
        /// <param name="commandTimeout"></param>
        /// <param name="options"></param>
        public MySqlDbContext(string connectionName, int commandTimeout, DbContextOptions options) : base(connectionName, commandTimeout, options)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="optionsBuilder"></param>
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            ServerVersion serverVersion = ServerVersion.AutoDetect(ConnectionString);

            optionsBuilder.UseMySql(ConnectionString, serverVersion, (builder) =>
            {
                builder.CommandTimeout(this.CommandTimeout);
                builder.DefaultDataTypeMappings(mappings =>
                    mappings.WithClrBoolean(MySqlBooleanType.Bit1).WithClrBoolean(MySqlBooleanType.TinyInt1)
                );

            });
        }
    }
}